package com.gln.bloger.action.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorActionForm;

import com.gln.bloger.pojos.Comment;
import com.gln.bloger.util.PagingSupport;
import com.gln.bloger.util.StringUtil;

/**
 * Created on 20080115
 * @author renxj
 * modify entries/articles 
 */
public class CommentAction extends UserBaseAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception {
		String p = (String)request.getParameter("p");
		PagingSupport<Comment> comments = getCommentService().
		                                  getAllComments(getAuthWebsite(request));
		if(!StringUtil.isBlank(p)){
			comments.setCurrentPage(Integer.parseInt(p));
		}
		request.setAttribute("comments", comments);
		return mapping.findForward("listCommentsPage");
	}
	
	public ActionForward approve(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception {
		String id = request.getParameter("id");
		this.getCommentService().approve(id);
		/**
		 * clear cached recent comment
		 */
		this.getCommentService().clearCachedRecentComments();
		return mapping.findForward("listComments");
	}
	public ActionForward remove(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception {
		DynaValidatorActionForm commentForm = (DynaValidatorActionForm) form;
		String [] ids = commentForm.getStrings("ids");
		String id = request.getParameter("id");
		if(ids != null && ids.length > 0){
			for(String ida : ids){
				getCommentService().remove(ida);
			}
		}else{
			getCommentService().remove(id);
		}
		/**
		 * clear cached recent comment
		 */
		this.getCommentService().clearCachedRecentComments();
		return mapping.findForward("listComments");
	}
}
